/*
 * Snapshots.java
 *
 * Created on January 8, 2008, 8:17 PM
 */

package picto.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import picto.io.Archivable;
import picto.io.ArchiveException;
import picto.io.ArchiveReader;
import picto.io.ArchiveWriter;

/**
 * Snapshots is a set of thumbnail images used to track progress of a 
 * work. 
 * @author davedes
 */
public class Snapshots implements Archivable, Iterable<ArchivableImage> {
    
    private List<ArchivableImage> thumbs = new ArrayList<ArchivableImage>();
    public static final String REF_PREFIX = "snapshot";
    private String imageFormat = null;
    
    /** Creates a new instance of Snapshots */
    public Snapshots() {
    }
    
    public ArchivableImage get(int i) {
        return thumbs.get(i);
    }
        
    public boolean add(ArchivableImage image) {
        return image!=null ? thumbs.add(image) : false;
    }
    
    public boolean remove(ArchivableImage image) {
        return image!=null ? thumbs.remove(image) : false;
    }
    
    public int size() {
        return thumbs.size();
    }
    
    public List<ArchivableImage> snapshots() {
        return thumbs;
    }
    
    public Iterator<ArchivableImage> iterator() {
        return thumbs.iterator();
    }
    
    
    public void dispose() {
        for (int i=0; i<thumbs.size(); i++) {
            thumbs.get(i).dispose();
        }
    }
    
    public Element write(ArchiveWriter writer) throws ArchiveException {
        Document doc = writer.getDocument();
        Element root = doc.createElement("snapshots");
        
        String defFormat = getImageFormat();
        if (defFormat!=null)
            root.setAttribute("format", defFormat);
        
        for (int i=0; i<thumbs.size(); i++) {
            ArchivableImage img = thumbs.get(i);
            if (defFormat!=null)
                img.setFormat(defFormat);
            img.setReference(REF_PREFIX+"_"+(i+1));
            Element child = img.write(writer);
            root.appendChild(child);
        }
        
        return root;
    }

    public void read(ArchiveReader reader, Element root) throws ArchiveException {
        if (!"snapshots".equals(root.getNodeName()))
            throw new IllegalArgumentException("Snapshots must be parsed from <snapshots> tag");
        
        this.thumbs.clear();
        
        String defFormat = root.getAttribute("format");
        if (defFormat.length()!=0)
            this.imageFormat = defFormat;
        
        NodeList nl = root.getChildNodes();
        for (int i=0, c=1; i<nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n instanceof Element && "image-data".equals(n.getNodeName())) {
                CachedImage img = new CachedImage(REF_PREFIX+"_"+c);
                img.read(reader, (Element)n);
                this.thumbs.add(img);
                c++;
            }
        }
    }

    public String getImageFormat() {
        return imageFormat;
    }

    public void setImageFormat(String imageFormat) {
        this.imageFormat = imageFormat;
    }
}
